package com.burpee.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.burpee.ui.GroupMember;
import com.burpee.ui.Groups;

@Repository(value="groupDao")
public class GroupsDaoImpl implements GroupsDao {

	SqlSessionTemplate sqlSessionTemplate;


	@Autowired
	public void setSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
	}

	public List<Groups> list(int currentPage, int onepageCount) {
		List<Groups> list = null;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("currentPage", currentPage);
		map.put("onepageCount", onepageCount);
		try {
			list = sqlSessionTemplate.selectList("grouplist",map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	public Groups view(long groupNo) {
		Groups groups = new Groups();

		try {
			groups = sqlSessionTemplate.selectOne("groupview", groupNo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return groups;
	}

	public Groups memberview(String memberId) {

		Groups groups = new Groups();

		try {
			groups = sqlSessionTemplate.selectOne("groupmypageview", memberId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return groups;
	}

	public int add(Groups groups) {
		int result = 0;
		try {
			result = sqlSessionTemplate.insert("groupadd",groups);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}

	public int modify(Groups groups) {
		int result = 0;

		try {
			result = (int)sqlSessionTemplate.update("groupmodify",groups);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}

	public int remove(long groupNo) {
		int result = 0;

		try {
			result = (int)sqlSessionTemplate.update("groupremove",groupNo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}
	public int countgroups() {
		Integer result = 0;
		
		result = (Integer)sqlSessionTemplate.selectOne("countgroups");
		
		return result.intValue();
	}
	public Groups groupcheck(String memberId){
		Groups groups = new Groups();
		
		try {
			groups = sqlSessionTemplate.selectOne("groupcheck",memberId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return groups;
	}

}
